(require 'cl)
(add-hook 'fortran-mode-hook 'turn-on-auto-fill)
;; (add-hook 'fortran-mode-hook 'outline-minor-mode)
(add-hook 'f90-mode-hook 'turn-on-auto-fill)

(global-set-key (kbd "C-c C-c") 'compile)

(setq skeleton-pair-alist  '(
;;    (?` ?` _ "''")
    (?\' _ "'")
    (?\` _ "`")
    (?\" _ "\"")
    (?\( _ ")")
    (?\[ _ "]")
    (?\{ _ "}")
    ))

;;; Fortran mode
(add-hook 'fortran-mode-hook 
          (lambda ()
            (abbrev-mode 1)
            (hs-minor-mode)
	    (outline-minor-mode)
            (fortran-auto-fill-mode 1)
            (imenu-add-menubar-index)
            (setq skeleton-pair t)
            (local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "\"") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "[") 'skeleton-pair-insert-maybe)
	    (local-set-key (kbd "M-.") `pop-tag-mark)
	    (local-set-key (kbd "C-.") `find-tag)
	    (local-set-key (kbd "<") 'self-insert-command)
            (unless (or (file-exists-p "makefile")
                        (file-exists-p "Makefile"))
              (set (make-local-variable 'compile-command)
                   (concat "gfortran -O3 "(buffer-name) " -o "
                           (file-name-sans-extension 
                            (file-name-nondirectory (buffer-name))))))))
(add-hook 'makefile-mode-hook
	  (lambda ()
	    (local-set-key (kbd "<f11>") 'compile)
	    ))
;;; Fortran90 mode
(add-hook 'f90-mode-hook 
          (lambda ()
            (abbrev-mode 1)
            (hs-minor-mode)
	    (outline-minor-mode)	    
            (auto-fill-mode 1)
            (f90-add-imenu-menu)
            (setq skeleton-pair t)
            (local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "\"") 'skeleton-pair-insert-maybe)
            (local-set-key (kbd "[") 'skeleton-pair-insert-maybe)
	    (local-set-key (kbd "M-.") `pop-tag-mark)
	    (local-set-key (kbd "C-.") `find-tag)
	    (local-set-key (kbd "<") 'self-insert-command)	    
            (unless (or (file-exists-p "makefile")
                        (file-exists-p "Makefile"))
              (set (make-local-variable 'compile-command)
                   (concat "gfortran -O3 "(buffer-name) " -o "
                           (file-name-sans-extension 
                            (file-name-nondirectory (buffer-name))))))))
;; set up the comment charact for
(setq fortran-comment-region "!")
(setq fortran-comment-line-start "!")

;------------ Some little personal script ---------------------------
(defun xwp-add-bar()
  (interactive)
  (insert-char ?- (- 72 (current-column))))
(fset 'delete-indent-fortran
   [?\C-a tab ?\C-3 backspace])
(add-hook 'fortran-mode-hook
	  '(lambda ()
	     (define-key fortran-mode-map (kbd "C-c C-c") 'smart-compile)
	     (define-key fortran-mode-map (kbd "C-c C-s") 'shell)
	     (define-key fortran-mode-map (kbd "C-\\") 'delete-indent-fortran)
	     (define-key fortran-mode-map (kbd "C-=") 'delete-blank-lines)
	     (define-key fortran-mode-map "\C-ct" 'hide-other)
	     (define-key fortran-mode-map "\C-ch" 'hs-hide-all)
	     (define-key fortran-mode-map "\C-ck" 'hide-subtree)
	     (define-key fortran-mode-map "\C-cj" 'show-entry)
	     (define-key fortran-mode-map "\C-cs" 'show-all)
	     (define-key fortran-mode-map "\C-co" 'show-subtree)
	     (define-key fortran-mode-map "\C-cc" 'xwp-add-bar)
	     (define-key fortran-mode-map "\C-o" 'hs-toggle-hiding)
	     (define-key fortran-mode-map "<f11>" 'compile)
	     (local-unset-key (quote [67108908]))
))
(add-hook 'f90-mode-hook
	  '(lambda ()
	     (define-key f90-mode-map (kbd "C-c C-c") 'smart-compile)
	     (define-key f90-mode-map (kbd "C-c C-s") 'shell)
	     (define-key f90-mode-map (kbd "C-\\") 'delete-indent-f90)
	     (define-key f90-mode-map (kbd "C-=") 'delete-blank-lines)
	     (define-key f90-mode-map "\C-ct" 'hide-other)
	     (define-key f90-mode-map "\C-ch" 'hs-hide-all)
	     (define-key f90-mode-map "\C-ck" 'hide-subtree)
	     (define-key f90-mode-map "\C-cj" 'show-entry)
	     (define-key f90-mode-map "\C-cs" 'show-all)
	     (define-key f90-mode-map "\C-co" 'show-subtree)
	     (define-key f90-mode-map "\C-cc" 'xwp-add-bar)
	     (define-key f90-mode-map "\C-o" 'hs-toggle-hiding)
	     (define-key f90-mode-map "\M-q" 'f90-fill-region)
	     (define-key f90-mode-map "<f11>" 'compile)	     
	     (local-unset-key (quote [67108908]))
))

(provide 'fortranmodel)
